package net.example.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import net.example.activity.adapter.FriendNotifyAdpter;
import net.example.activity.adapter.interfaces.IHandleOnClickCallback;
import net.example.manager.ClientGlobalCache;
import net.example.manager.Manager;
import net.example.model.AddFriendRequest;
import net.example.model.AddFriendRequestPage;
import net.example.model.FriendNotifyModel;
import net.example.model.GGUser;
import net.example.model.event.GetAddFriendPageEvent;
import net.example.model.event.HandleRequestAddFriendReceivedEvent;
import net.example.ggtalk.R;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class FriendNotifyActivity extends Activity implements IHandleOnClickCallback<FriendNotifyModel>
{
    private FriendNotifyAdpter adpter;
    private ListView listView;
    private List<FriendNotifyModel> modelList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_notify);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        ((View)findViewById(R.id.ct_photo)).setVisibility(View.GONE);
        TextView title = (TextView) findViewById(R.id.headerText);
        title.setText("新朋友");
        Manager.getInstance().getAddFriendPage();
        listView = (ListView) findViewById(R.id.lv_friendNotify);
        adpter = new FriendNotifyAdpter(this, modelList, R.layout.friend_request_item);
        adpter.setHandleOnClickCallback(this);
        listView.setAdapter(adpter);
    }

    public void onEventMainThread(GetAddFriendPageEvent event)
    {
        modelList.clear();
        AddFriendRequestPage page = event.getAddFriendRequestPage();
        if (page == null || page.AddFriendRequestList == null) {
            adpter.notifyDataSetChanged();
            return;
        }
        String currentUserID=ClientGlobalCache.getInstance().getCurrentUser().getUserID();
        for (AddFriendRequest item : page.AddFriendRequestList) {
            GGUser friend;
            if (item.RequesterID.equals(currentUserID)) {
                friend = ClientGlobalCache.getInstance().getUser(item.AccepterID);
                modelList.add(new FriendNotifyModel(friend, true, item));
            } else {
                friend = ClientGlobalCache.getInstance().getUser(item.RequesterID);
                modelList.add(new FriendNotifyModel(friend, false, item));
            }
        }
        adpter.notifyDataSetChanged();
    }


    public void finish(View view)
    {
        finish();
    }

    @Override
    protected void onDestroy()
    {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onAgreeClick(FriendNotifyModel friendNotifyModel) {
        //默认加到我的好友列表
        Manager.getInstance().handleAddFriendRequest(friendNotifyModel.AddFriendRequest.RequesterID,getResources().getString(R.string.friend_catalog_name),true);
        ClientGlobalCache.getInstance().removeRequestAddFriend(friendNotifyModel.AddFriendRequest.RequesterID);
        friendNotifyModel.AddFriendRequest.State=1;
        adpter.notifyDataSetChanged();
    }

    @Override
    public void onRefuseClick(FriendNotifyModel friendNotifyModel) {
        Manager.getInstance().handleAddFriendRequest(friendNotifyModel.AddFriendRequest.RequesterID,"",false);
        ClientGlobalCache.getInstance().removeRequestAddFriend(friendNotifyModel.AddFriendRequest.RequesterID);
        friendNotifyModel.AddFriendRequest.State=2;
        adpter.notifyDataSetChanged();
    }

    public void onEventMainThread(HandleRequestAddFriendReceivedEvent event)
    {
        Manager.getInstance().getAddFriendPage();
    }
}
